<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>学生信息管理系统</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/element-ui@2.15.13/lib/theme-chalk/index.css">
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/element-ui@2.15.13/lib/index.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
    <div id="app">
        <el-container>
            <el-main>
                <h2>学生信息管理系统</h2>
                
                <!-- 搜索和添加区域 -->
                <div style="margin-bottom: 20px">
                    <el-input
                        v-model="searchQuery"
                        placeholder="请输入关键字搜索"
                        style="width: 200px"
                        @keyup.enter.native="handleSearch">
                    </el-input>
                    <el-button type="primary" @click="handleSearch">搜索</el-button>
                    <el-button type="success" @click="showAddDialog">添加学生</el-button>
                </div>

                <!-- 数据表格 -->
                <el-table
                    v-loading="loading"
                    :data="tableData"
                    border
                    style="width: 100%">
                    <el-table-column prop="id" label="ID" width="80"></el-table-column>
                    <el-table-column prop="student_number" label="学号" width="120"></el-table-column>
                    <el-table-column prop="name" label="姓名" width="120"></el-table-column>
                    <el-table-column prop="age" label="年龄" width="80"></el-table-column>
                    <el-table-column prop="class_name" label="班级" width="120"></el-table-column>
                    <el-table-column prop="gender" label="性别" width="80"></el-table-column>
                    <el-table-column prop="hometown" label="籍贯"></el-table-column>
                    <el-table-column label="操作" width="180">
                        <template slot-scope="scope">
                            <el-button size="mini" @click="showEditDialog(scope.row)">编辑</el-button>
                            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <!-- 分页器 -->
                <div style="margin-top: 20px">
                    <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page.sync="pagination.currentPage"
                        :page-sizes="[10, 20, 50, 100]"
                        :page-size="pagination.pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="pagination.total">
                    </el-pagination>
                </div>

                <!-- 添加/编辑对话框 -->
                <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
                    <el-form :model="form" ref="form" label-width="100px">
                        <el-form-item label="学号" prop="student_number">
                            <el-input v-model="form.student_number"></el-input>
                        </el-form-item>
                        <el-form-item label="姓名" prop="name">
                            <el-input v-model="form.name"></el-input>
                        </el-form-item>
                        <el-form-item label="年龄" prop="age">
                            <el-input-number v-model="form.age" :min="1"></el-input-number>
                        </el-form-item>
                        <el-form-item label="班级" prop="class_name">
                            <el-input v-model="form.class_name"></el-input>
                        </el-form-item>
                        <el-form-item label="性别" prop="gender">
                            <el-select v-model="form.gender" style="width: 100%">
                                <el-option label="男" value="男"></el-option>
                                <el-option label="女" value="女"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="籍贯" prop="hometown">
                            <el-input v-model="form.hometown"></el-input>
                        </el-form-item>
                    </el-form>
                    <div slot="footer">
                        <el-button @click="dialogVisible = false">取 消</el-button>
                        <el-button type="primary" @click="handleSubmit">确 定</el-button>
                    </div>
                </el-dialog>
            </el-main>
        </el-container>
    </div>

    <script>
        new Vue({
            el: '#app',
            data() {
                return {
                    loading: false,
                    searchQuery: '',
                    tableData: [],
                    pagination: {
                        currentPage: 1,
                        pageSize: 10,
                        total: 0
                    },
                    dialogVisible: false,
                    dialogTitle: '',
                    form: {
                        id: null,
                        student_number: '',
                        name: '',
                        age: 18,
                        class_name: '',
                        gender: '男',
                        hometown: ''
                    }
                }
            },
            created() {
                this.fetchData()
            },
            methods: {
                // 获取数据
                fetchData() {
                    this.loading = true
                    console.log('Fetching data with params:', {
                        page: this.pagination.currentPage,
                        pageSize: this.pagination.pageSize,
                        search: this.searchQuery
                    })

                    axios.get('/api/students/list', {
                        params: {
                            page: this.pagination.currentPage,
                            pageSize: this.pagination.pageSize,
                            search: this.searchQuery
                        }
                    })
                    .then(response => {
                        console.log('Received response:', response.data)
                        if (response.data.code === 0 && response.data.data) {
                            // 确保数据格式正确
                            this.tableData = response.data.data.items.map(item => ({
                                id: Number(item.id),
                                student_number: String(item.student_number),
                                name: String(item.name),
                                age: Number(item.age || 0),
                                class_name: String(item.class_name || ''),
                                gender: String(item.gender),
                                hometown: String(item.hometown || '')
                            }))
                            this.pagination.total = Number(response.data.data.total)
                            console.log('Processed table data:', this.tableData)
                        } else {
                            this.$message.error(response.data.message || '获取数据失败')
                        }
                    })
                    .catch(error => {
                        console.error('Error fetching data:', error)
                        this.$message.error('获取数据失败：' + error.message)
                    })
                    .finally(() => {
                        this.loading = false
                    })
                },

                // 搜索
                handleSearch() {
                    this.pagination.currentPage = 1
                    this.fetchData()
                },

                // 分页
                handleSizeChange(val) {
                    this.pagination.pageSize = val
                    this.fetchData()
                },
                handleCurrentChange(val) {
                    this.pagination.currentPage = val
                    this.fetchData()
                },

                // 显示添加对话框
                showAddDialog() {
                    this.dialogTitle = '添加学生'
                    this.form = {
                        id: null,
                        student_number: '',
                        name: '',
                        age: 18,
                        class_name: '',
                        gender: '男',
                        hometown: ''
                    }
                    this.dialogVisible = true
                },

                // 显示编辑对话框
                showEditDialog(row) {
                    this.dialogTitle = '编辑学生'
                    this.form = {...row}
                    this.dialogVisible = true
                },

                // 提交表单
                handleSubmit() {
                    if (this.form.id) {
                        // 更新
                        axios.put(`/api/students/update/${this.form.id}`, this.form)
                            .then(response => {
                                if (response.data.code === 0) {
                                    this.$message.success('更新成功')
                                    this.dialogVisible = false
                                    this.fetchData()
                                } else {
                                    this.$message.error(response.data.message)
                                }
                            })
                            .catch(error => {
                                this.$message.error('更新失败：' + error.message)
                            })
                    } else {
                        // 添加
                        axios.post('/api/students/add', this.form)
                            .then(response => {
                                if (response.data.code === 0) {
                                    this.$message.success('添加成功')
                                    this.dialogVisible = false
                                    this.fetchData()
                                } else {
                                    this.$message.error(response.data.message)
                                }
                            })
                            .catch(error => {
                                this.$message.error('添加失败：' + error.message)
                            })
                    }
                },

                // 删除
                handleDelete(row) {
                    this.$confirm('确认删除该学生信息吗？', '提示', {
                        type: 'warning'
                    }).then(() => {
                        axios.delete(`/api/students/delete/${row.id}`)
                            .then(response => {
                                if (response.data.code === 0) {
                                    this.$message.success('删除成功')
                                    this.fetchData()
                                } else {
                                    this.$message.error(response.data.message)
                                }
                            })
                            .catch(error => {
                                this.$message.error('删除失败：' + error.message)
                            })
                    }).catch(() => {})
                }
            }
        })
    </script>
</body>
</html>